package com.myapp.myhome.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVObject;
import com.myapp.myhome.R;

public class MySerchAdapter extends BaseAdapter {

	LayoutInflater layout;
	List<AVObject> avObjects = new ArrayList<AVObject>();
	MyViewHolder holder;
	Context context;
	String str_name;

	public MySerchAdapter(Context context) {
		layout = LayoutInflater.from(context);
		this.context = context;
		str_name = "";
	}

	public void setName(String str_name) {
		this.str_name = str_name;
	}

	public void setData(List<AVObject> avObjects) {
		this.avObjects = avObjects;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return avObjects.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		// current menu type
		AVObject list = avObjects.get(position);
		boolean state = list.getBoolean("paystate");
		if (state) { // 已支付
			return 1;
		} else {// 未支付
			return 0;
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = layout.inflate(R.layout.search_all_item, null);
			holder = new MyViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (MyViewHolder) convertView.getTag();
		}
		final AVObject list = avObjects.get(position);

		holder.text_date.setText(list.getString("date"));
		holder.text_money.setText(list.getString("money"));
		
		
		if (!str_name.equals("")) {
			SpannableStringBuilder builder = new SpannableStringBuilder(
					list.getString("who"));
			ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
			int index = list.getString("who").indexOf(str_name);
			if(index!=-1){
				builder.setSpan(redSpan, index, index + str_name.length(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				holder.text_who.setText(builder);
			}
			if(str_name.equals("全部")){
				holder.text_who.setText(list.getString("who"));
			}
		} else {
			holder.text_who.setText(list.getString("who"));
		}

		
		
		boolean state = list.getBoolean("paystate");
		if (state) {
			holder.text_state.setText("已结算");

		} else {
			holder.text_state.setText("未结算");
			holder.text_state.setTextColor(Color.rgb(0x04, 0x98, 0xd8));
		}
		// 预付人
		final String str_whopay = list.getString("whopay");
		if (str_whopay != null) {
			holder.text_whopay.setText(str_whopay);
		} else {
			holder.text_whopay.setText("");
		}
		final String str_note = list.getString("note");
		if (str_note != null && !str_note.equals("")) {
			holder.mRelativeLayout.setVisibility(View.VISIBLE);
			holder.text_note.setText(str_note);
		} else {
			holder.mRelativeLayout.setVisibility(View.GONE);
		}
		return convertView;
	}

	class MyViewHolder {
		TextView text_date;
		TextView text_money;
		TextView text_who;
		TextView text_state;
		TextView text_whopay;
		RelativeLayout mRelativeLayout;
		TextView text_note;

		public MyViewHolder(View view) {
			text_date = (TextView) view.findViewById(R.id.search_all_item_date);
			text_money = (TextView) view
					.findViewById(R.id.search_all_item_money);
			text_who = (TextView) view.findViewById(R.id.search_all_item_who);
			text_state = (TextView) view
					.findViewById(R.id.search_all_item_state);
			text_whopay = (TextView) view
					.findViewById(R.id.search_all_item_whopay);
			mRelativeLayout = (RelativeLayout) view
					.findViewById(R.id.search_all_item_le_note);
			text_note = (TextView) view
					.findViewById(R.id.search_all_item_text_note);
		}

	}
}
